package ldh.text.pane;

import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import ldh.text.controller.BigDataTextAreaController;
import ldh.text.service.CacheDataService;
import ldh.text.service.CursorDataService;
import ldh.text.util.FxmlNodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class BigDataTextAreaPane extends StackPane {

    private static final Logger LOGGER = LoggerFactory.getLogger(BigDataTextAreaPane.class);

    private static final int CACHE_SIZE = 20 * 1024 * 1024;  // 20m
    private static final int MORE_DATA_SIZE = 1024 * 1024;   // 1m
    private static final int FIND_MAX_SIZE = 500;          // 查找显示个数，超过这个数量，请优化查找关键字

    private File file;
    private CursorDataService dataService;

    private BigDataTextAreaController bigDataTextAreaController;

    public BigDataTextAreaPane(File file) {
        this.file = file;
        try {
            FxmlNodeUtil.FxmlNode<VBox, BigDataTextAreaController> fxmlNode = FxmlNodeUtil.load(this.getClass().getResource("/fxml/BigDataTextArea.fxml"));
            this.bigDataTextAreaController = fxmlNode.getController();
            this.getChildren().add(fxmlNode.getNode());
            dataService = new CacheDataService(file, CACHE_SIZE);

            this.bigDataTextAreaController.setDataService(dataService, CACHE_SIZE, MORE_DATA_SIZE, FIND_MAX_SIZE);
        } catch (Exception e) {
            LOGGER.error("get file size error!", e);
        }
    }

    public void close() {
        try {
            dataService.close();
        } catch (Exception e) {
            LOGGER.error("DataService close error", e);
        }
    }

    public void focus(){
        bigDataTextAreaController.focus();
    }

    public void loadData(boolean isWrapText) {
        this.bigDataTextAreaController.loadData(isWrapText);
    }

    public void setWrapText(boolean selected) {
        this.bigDataTextAreaController.setWrapText(selected);
    }
}
